package day11;
//死锁：实现两个线程 持有多个锁 让其相互持有对方的锁 从而造成死锁
public
class Deadlock {
    public static
    void main(String[] args) {
        String s1="123";
        String s2="456";
        new Thread(()->{
            synchronized (s1){
                System.out.println(Thread.currentThread().getName()+"获得s1锁,等待s2锁");
                synchronized (s2){
                    System.out.println(Thread.currentThread().getName()+"获得s1,s2锁");
                }
                System.out.println(Thread.currentThread().getName()+"释放s2锁");
            }
            System.out.println(Thread.currentThread().getName()+"释放s1锁");
        },"线程1").start();
        new Thread(()->{
            synchronized (s2){
                System.out.println(Thread.currentThread().getName()+"获得s2锁,等待s1锁");
                synchronized (s1){
                    System.out.println(Thread.currentThread().getName()+"获得s1,s2锁");
                }
                System.out.println(Thread.currentThread().getName()+"释放s1锁");
            }
            System.out.println(Thread.currentThread().getName()+"释放s2锁");
        },"线程2").start();
    }
}
